﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CetWin.Phone.Entity;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using Phone.Framework.Collections;
namespace CetWin.Phone.Services
{
    public class HistoryService : BaseDataService
    {

        public IList<Favorite> GetHistories()
        {
            return new ObservableCollection<Favorite>(db.Favorites);
        }

        public bool UpdateTime(string text)
        {
            var contains = Contains(text);
            if (contains)
            {
                var entity = db.Favorites.FirstOrDefault(e => e.Name == text);
                entity.Time = DateTime.Now;
                Update();
            }
            return contains;
        }

        public bool Contains(string text)
        {
            return db.Favorites.Any(e => e.Name == text);
        }

        public void AddHistory(string text)
        {
            var entity = new Favorite() { Name = text, Time = DateTime.Now };
            db.Favorites.InsertOnSubmit(entity);
            Update();
        }

        public void Clear()
        {
            db.Favorites.DeleteAllOnSubmit(db.Favorites);
            Update();
        }

        public int Count()
        {
            return db.Favorites.Count();
        }

        public int SearchRecordsCount()
        {
            return db.Histories.Count();
        }

        public IEnumerable GetGroupHistory()
        {
            var groupData = from movie in db.Favorites
                            group movie by
                            new DateTime(movie.Time.Year, movie.Time.Month, movie.Time.Day) into c
                            orderby c.Key descending
                            select new PublicGrouping<DateTime, Favorite>(c);
            return groupData;
        }

        public void DeleteHistory(string text)
        {
            var entity = db.Favorites.FirstOrDefault(e => e.Name == text);
            db.Favorites.DeleteOnSubmit(entity);
            Update();
        }

        public void DeleteHistory(Favorite entity)
        {
            db.Favorites.DeleteOnSubmit(entity);
            Update();
        }

        public void SaveHistory(string name, bool isNotExisted)
        {
            if (isNotExisted)
            {
                AddHistory(name);
            }
            else
            {
                DeleteHistory(name);
            }
        }

        public void UpdateSearchRecords(string name)
        {
            var existed = db.Histories.Any(e => e.Name == name);
            History entity = null;
            if (existed)
            {
                entity = db.Histories.FirstOrDefault<History>(e => e.Name == name);
               
                entity.Time = DateTime.Now;
                entity.Count++;
                Update();
               
            }
            else
            {
                entity = new History() { Name = name, Time = DateTime.Now,Count=1 };
                db.Histories.InsertOnSubmit(entity);
                Update();
            }
        }

        public void ClearSearchRecords()
        {
            db.Histories.DeleteAllOnSubmit(db.Histories);
            Update();
        }

        public void DeleteSearchRecords(History entity)
        {
            db.Histories.DeleteOnSubmit(entity);
            Update();
        }

        public IEnumerable GetSearchRecords()
        {
            return db.Histories.OrderBy(e=>e.Time);
        }
    }
}
